package com.cloudsigns.factorymanager2.project.model;

import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.produce.bean.Produce_ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class AddProduceOrderModel implements IAddProduceOrderModel {

    @Override
    public void addProduceOrderModel(final long completeDate,
                                     final String engineeringName,
                                     final String isHurry,
                                     final String level,
                                     final List<Produce_ProductBean> productList,
                                     final String projectName,
                                     final String type,
                                     final OnAddProduceOrderListener onAddProduceOrderListener) {
        new Thread(new Runnable() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.ADD_PRODUCT_ORDER);

                HttpResponse response;
                try {
                    // 添加http头信息
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("completeDate", completeDate);
                    jsonObject.put("engineeringName", engineeringName);
                    jsonObject.put("isHurry", isHurry);
                    jsonObject.put("level", level);

                    if (null != productList && productList.size() > 0) {
                        JSONArray products = new JSONArray();

                        for (int i = 0; i < productList.size(); i++) {
                            Produce_ProductBean productBean = productList.get(i);
                            JSONObject productObj = new JSONObject();
                            productObj.put("amount", productBean.getAmount());
                            productObj.put("name", productBean.getProductName());

                            List<String> picUrlLs = productBean.getPicUrl();
                            JSONArray picUrlJsonArr = new JSONArray();
                            if (null != picUrlLs && picUrlLs.size() > 0) {

                                for (String picpath : picUrlLs) {
                                    picUrlJsonArr.put(picpath);
                                }
                            }
                            productObj.put("picUrl", picUrlJsonArr);

                            productObj.put("shapeMap", productBean.isShapeMap());

                            JSONObject shapeTypeObj = new JSONObject();
                            shapeTypeObj.put("border", productBean.getShapeType().getBorder());
                            shapeTypeObj.put("borderBottom", productBean.getShapeType().getBorderBottom());
                            shapeTypeObj.put("borderLeft", productBean.getShapeType().getBorderLeft());
                            shapeTypeObj.put("borderRight", productBean.getShapeType().getBorderRight());
                            shapeTypeObj.put("borderTop", productBean.getShapeType().getBorderTop());
                            shapeTypeObj.put("desc", productBean.getShapeType().getDesc());
                            shapeTypeObj.put("diameter", productBean.getShapeType().getDiameter());
                            shapeTypeObj.put("height", productBean.getShapeType().getHeight());
                            shapeTypeObj.put("thick", productBean.getShapeType().getThick());
                            if (null != productBean.getShapeType().getType()) {
                                CommonEnum.ProductShapeType type = productBean.getShapeType().getType();
                                if (type == CommonEnum.ProductShapeType.RECTANGLE) {
                                    shapeTypeObj.put("type", "RECTANGLE");
                                } else if (type == CommonEnum.ProductShapeType.ROUND) {
                                    shapeTypeObj.put("type", "ROUND");

                                } else if (type == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                                    shapeTypeObj.put("type", "SPECIALSHAPE");
                                }
                            }
                            shapeTypeObj.put("width", productBean.getShapeType().getWidth());
                            productObj.put("shapeType", shapeTypeObj);

                            productObj.put("unit", productBean.getUnit());

                            products.put(productObj);
                        }
                        jsonObject.put("productList", products);
                    } else {
                        jsonObject.put("productList", new JSONArray());
                    }
                    jsonObject.put("projectName", projectName);
                    jsonObject.put("type", type);

                    Log.e("AddProduceOrderModel","jsonObject="+jsonObject.toString());

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.setEntity(se);
                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    Log.e("AddProduceOrderModel","result="+result);
                    Log.e("AddProduceOrderModel","code="+code);
                    if (null != onAddProduceOrderListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            onAddProduceOrderListener.success(json.getString("id"));
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            onAddProduceOrderListener.failed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
            }
        }).start();
    }


    public interface OnAddProduceOrderListener {
        void success(String orderId);

        void failed(String desc);
    }

}
